<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit='formSubmit'
	 :showHistoryBtn="true" >
		<om-autoform v-if="mainConfigData.length" ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
		<om-sub :title="ms_title" :subTitle="ms_subtitle">
			<uni-list >
				<view v-for="(item,index) in workList" :key="index">
					<uni-list-item 
					:showArrow="true" 
					@click="openWork(index)" 
					:avatar="index + 1"
					:showClear="true"
					 @iconClear="delSub(index)">
						<view>{{item.workName3 + "("+ (item.maxScore?item.maxScore:"未设评") +"分)"}}</view>
						<view class="text-gray text-sm padding-bottom-xs padding-top-xs">{{item.workOutput3}}</view>
						<view class="text-gray text-sm">{{item.bearUser3Name + " | 计划" + item.planEnd3 + "完成"}}</view>
					</uni-list-item>
				</view>
			</uni-list>
			<view v-if="subConfigData.length" class="tui-flex align-center justify-center padding-top">
				<text class="lg text-green cuIcon-add" @click="openWork()">{{"增加"+ms_title}}</text>
			</view>
		</om-sub>
		<om-white-space bgColor="#eee"></om-white-space>
	</om-addpage>
</template>

<script>
	import {getUrl, confirmModal} from '@/static/js/common.js';
	import {getFormFields,formValidation,tranferToEditors,changeEditor,configSelectData} from '@/static/js/formUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return {
				menuName: '',
				id: '',
				busCode: 'OBJ0504',
				formdata: {
					
				},
				mainConfigData:[],
				subConfigData:[],
				initValue:{
					monthNum:'',
					objStr3:"",
					busCode:'OBJ0504',
				},
				workCacheKey:"obj-m-inst-work-cache",
				workConfigDataCacheKey:"obj-m-inst-work-configdata-cache",
				workList:[],
				ms_title:"明细",
				ms_subtitle:"",
				scoreCode:"DD021017003"
			}
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId', 'salaryPosName'])
		},
		watch:{
			workList:function(newValue, oldValue){
				if(newValue && newValue.length){
					if(this.$refs.autoform){
					this.$refs.autoform.formdata.objStr3 = JSON.stringify(newValue)
					}else{
						this.initValue.objStr3 = JSON.stringify(newValue)
					}
				}else{
					if(this.$refs.autoform){
						this.$refs.autoform.formdata.objStr3 = ""
					} else {
						this.initValue.objStr3 = ""
					}
				}
			}
		},
		methods: {
			formSubmit(){
				var checkRes = formValidation(this.$refs.autoform.configData, this.formdata);
				if (checkRes) {
					this.om.toast(checkRes);
				} else {
					if(!this.formdata.objStr3){
						this.om.toast("请配置" + this.ms_title)
						return
					}
					this.om.formSave('tinstMonthlyTargetWork/addTinstMonthlyTargetWork', this.formdata, this)
				}
			},
			openWork(index){
				if(!this.salaryPosName){
					this.om.toast("没有找到您的计酬岗位")
				}else{
					uni.setStorage({
					  key: this.workConfigDataCacheKey,
					  data: this.subConfigData
					})
					uni.setStorage({
					  key: this.workCacheKey,
					  data: this.workList
					})
					var param = {
						configDataCacheKey:this.workConfigDataCacheKey,
						cachename:this.workCacheKey,
						index:index,
						year:this.formdata.year,
						monthNum:this.formdata.monthNum,
					}
					var url = getUrl("data-info",param)
					uni.navigateTo({
						url: url
					});
				}
			},
			delSub(index){
				if(index != null) {
					this.workList.splice(index,1)
				}
			},
			onSelectYear(item){
				let _that = this
				this.om.request('tinstMonthlyTargetWork/verifyYear', {year:item.value}).then(res=>{
					if(res.succeed && res.value){
						confirmModal(_that, "提示信息", res.value, function(){
							 _that.monthFunc(item);
						}, function(){
							_that.$refs.autoform.formdata.year = ""
						})
					}else{
						_that.monthFunc(item);
					}
				})
			},
			monthFunc(item){
				let _that = this
				this.om.request('dictZero/getRecentMonth', {year:item.value}).then(res=>{
					//if(res instanceof Array){
					if(Object.prototype.toString.call(res)==='[object Array]'){
						_that.$refs.autoform.changeEditor("topinfo","monthCode","data",configSelectData(res, "code","dicname"))
					}
				})
			},
			onSelectMonth(item){
				this.$refs.autoform.formdata.monthNum = item.referNum
			},
			initLoad(options){
				let _that = this
				if(!this.busCode) {
					return
				}
				let fileId = options.fileId
				var pro3 = ""
				if(fileId){
					pro3 = this.om.request('tinstMonthlyTargetWork/getInfo',{
						id:fileId
					})
				}
				var pro1 = this.om.request('formFieldConfig/showPage', {code:this.busCode});
				var pro2 = this.om.request('dataDict/getIntByCode',{code:_that.scoreCode})
				//多个AJAX同时完成时开始执行
				Promise.all([pro1,pro2,pro3]).then(res=>{
					if (res[0].succeed) {
						let data = res[0].value
						var mainData = [{
							"key":"topinfo",
							editors:tranferToEditors(data.top)
						}]
						var date = new Date()
						var currentYear = date.getFullYear()
						changeEditor(mainData[0].editors,"year","data",[{text:"请选择"},{
								value:currentYear,
								text:currentYear
							},{
								value:currentYear+1,
								text:currentYear+1
							}],
						)
						changeEditor(mainData[0].editors,"zposName","defaultValue",_that.salaryPosName)
						//赋值
						_that.mainConfigData = mainData
						if (data.bodyTitle.length > 0) {
							_that.ms_title = data.bodyTitle[0].itemName
							for (var i = 0; i < data.bodyTitle.length; i++) {
								var datas = data["body" + i];
								_that.subConfigData.push({
									title:data.bodyTitle[i].itemName,
									key:data.bodyTitle[i].fieldId,
									issub:true,
									editors:tranferToEditors(datas)
								})
							}
						}
						changeEditor(_that.subConfigData[0].editors, 'maxScore', 'defaultValue', res[1].value);
						changeEditor(_that.subConfigData[0].editors, 'maxScore', 'max', res[1].value);
						changeEditor(_that.subConfigData[0].editors, 'maxScore', 'prompt', "介于1~"+res[1].value+"之间");
						if(fileId){
							let fieldArr = getFormFields(mainData)
							fieldArr.push('id')
							for(let index in res[2].value.tinstMonthlyTargetWork){
								if(fieldArr.includes(index)){ 
									_that.$set(_that.formdata,index,res[2].value.tinstMonthlyTargetWork[index])
								}
							}
							var dataList = _that._.cloneDeep(res[2].value.tkeyWork_data)
							_that.workList = dataList
							_that.$set(_that.formdata, "objStr", JSON.stringify(dataList))
							delete res[2].value.dataList
							_that.$nextTick(function(){
								changeEditor(mainData[0].editors,"year","data",[{text:res[2].value.tinstMonthlyTargetWork.year,value:res[2].value.tinstMonthlyTargetWork.year}])
								changeEditor(mainData[0].editors,"monthCode","data",[{text:res[2].value.tinstMonthlyTargetWork.monthName,value:res[2].value.tinstMonthlyTargetWork.monthCode}])
								_that.$refs.autoform.formdata.year = res[2].value.tinstMonthlyTargetWork.year
								_that.$refs.autoform.formdata.monthName = res[2].value.tinstMonthlyTargetWork.monthName
								_that.$refs.autoform.formdata.monthCode = res[2].value.tinstMonthlyTargetWork.monthCode
								
							})
						}
					}else{
						_that.om.toast(res[0].msg)
					}
				});
			},
		},
		onLoad(options){
			let _that = this;
			_that.menuName = options.title || '月工作计划';
			uni.setNavigationBarTitle({
				title: _that.menuName
			});
			_that.busCode = options.code;
			_that.formdata.userId = this.userId
			uni.removeStorage({
				key: _that.workCacheKey
			})
			_that.initLoad(options);
			
		},
		onShow() {
			let _that = this
			uni.getStorage({
				key: _that.workCacheKey,
				success: function(res) {
					_that.workList = res.data;
					uni.removeStorage({
						key: _that.workCacheKey
					})
				}
			});
		}
	}
</script>
<style>
</style>
